/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2013/11/2 星期六 9:11:54                     */
/*==============================================================*/

<#list classMetas as classMeta>
drop table if exists `${classMeta.tableName}`;
</#list>


<#list classMetas as classMeta>
/*==============================================================*/
/* Table: `${classMeta.tableName}`                                             */
/*==============================================================*/
create table `${classMeta.tableName}`
(
    <#list classMeta['fieldMetas'] as fieldMeta>
        <#if fieldMeta.columnName??>
        `${fieldMeta.columnName!}`   ${fieldMeta.type!}<#if fieldMeta.length!=0>(${fieldMeta.length!})</#if><#if fieldMeta.null==false> not null </#if>  <#if fieldMeta.primaryKey==false> ${fieldMeta.defaultValue!} </#if><#if fieldMeta.primaryKey!=false>primary key auto_increment</#if> <#if fieldMeta_has_next>,</#if>
        </#if>
    </#list>
);
</#list>


<#list classMetas as classMeta>
    <#list classMeta['fieldMetas'] as fieldMeta>
        <#if fieldMeta.foreignKeyMeta??>
        alter table `${classMeta.tableName}` add constraint `${fieldMeta.foreignKeyMeta.foreignKeyName!}` foreign key (`${fieldMeta.foreignKeyMeta.foreignKeyId!}`)  references `${fieldMeta.foreignKeyMeta.referencesTableName!}`(`${fieldMeta.foreignKeyMeta.referencesId!}`) on delete restrict on update restrict;
        </#if>
    </#list>
</#list>
